//
//  SuperviseTaskDetailVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//
// 【首页-检查监督】检查任务-任务报告详情

import Foundation
import UIKit

class SuperviseTaskDetailVC: SubLevelViewController {
    
    let taskV = SuperviseTaskDetailView()
    
    var modal: SpRwbgxqModal?
    
    var id: Int64!
    
    init(id: Int64!) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "任务报告详情"
        navigationController?.navBarStyle(.white)
        view.backgroundColor = .bg
        
        setupUI()
        
        reloadData()
        reloadCommons()
    }
    
    func reloadData() {
        API.getJcjdRwbgxq(withParam: SpDetailParam(id: id)) { responseModel in
            DispatchQueue.main.async {
                self.modal = responseModel.model
                self.taskV.reload(withModal: self.modal)
            }
        }
    }
    
    func reloadCommons() {
        API.getJcjdRwpllb(withParam: SpDetailParam(id: id)) { responseModel in
            DispatchQueue.main.async {
                self.taskV.reload(withCommons: responseModel.models ?? [])
            }
        }
    }
    
    // 检查基本信息
    @objc func basicTapped() {
        navigationController?.pushViewController(SuperviseTaskBasicVC(modal: modal), animated: true)
    }
    
    // 检查情况
    @objc func checkTapped() {
        navigationController?.pushViewController(SuperviseTaskCheckDetailVC(id: id), animated: true)
    }
    
    // 调整情况
    @objc func adjustTapped() {
        navigationController?.pushViewController(SuperviseTaskIssueVC(id: id), animated: true)
    }
    
    // 发表评论
    @objc func addCommonTapped() {
        API.getJcjdRwpltj(withParam: SpDetailParam(rwjlid: id, plnr: taskV.writeTV.text)) { responseModel in
            DispatchQueue.main.async {
                self.taskV.writeTV.text = ""
                self.reloadCommons()
            }
        }
    }
    
    // MARK: - Setup
    
    func setupUI() {
        taskV.checkBV.addTarget(self, action: #selector(checkTapped), for: .touchUpInside)
        taskV.adjustBV.addTarget(self, action: #selector(adjustTapped), for: .touchUpInside)
        taskV.detailBtn.addTarget(self, action: #selector(basicTapped), for: .touchUpInside)
        taskV.submitBtn.addTarget(self, action: #selector(addCommonTapped), for: .touchUpInside)
        view.addSubview(taskV)
        taskV.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.left.right.equalToSuperview()
        }
    }
}
